﻿using admin.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace admin.Repository
{
    public class AdminRepository
    {
        protected List<Administrator> Admins { get; set; }

        public AdminRepository()
        {
            this.Admins = new List<Administrator>();
        }

        //查询管理员
        public Administrator QueryAdmin(string Id)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                var admin = dbContext.Administrator.FirstOrDefault(x => x.Id == Id);
                //dbContext.SaveChanges();
                return admin;
            }
        }

        //更新管理员
        public Administrator UpdateAdmin(Administrator admin)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                var repo = dbContext.Administrator.Update(admin);
                dbContext.SaveChanges();
                if (repo != null)
                    return admin;
                else
                    return null;
            }
        }
        ////销量统计
        //public string GetBookSaleCount()
        //{
        //    using(var dbContext = new pressmannersystemContext())
        //    {
        //        //var salecount = (from u in dbContext.Book group ).ToString();
        //        return salecount;
        //    }
            
        //}

        //得到所有图书
        public List<Book> GetAllBook()
        {
            List<Book> bookList = new List<Book>();
            using(var dbContext = new pressmannersystemContext())
            {
                bookList = (from u in dbContext.Book
                            select u).ToList();
                dbContext.SaveChanges();
            }
            return bookList;
        }

        //查询图书
        public Book QueryBook(string id)
        {
            using(var dbContext = new pressmannersystemContext())
            {
                var bookShow = dbContext.Book.FirstOrDefault(x => x.Id == id);
                dbContext.SaveChanges();
                return bookShow;
            }
        }

        //删除图书
        public bool DelBook(string id)
        {
            using(var dbContext = new pressmannersystemContext())
            {
                var book =dbContext.Book.FirstOrDefault(x => x.Id == id);
                if (book != null)
                {
                    if (dbContext.Book.Remove(book) != null)
                    {
                        dbContext.SaveChanges();
                        return true;
                    }
                }
                return false;
            }
        }

        //查询用户
        public Customers QueryCustomer(string id)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                var customer = dbContext.Customers.Find(id);
                dbContext.SaveChanges();
                return customer;
            }
        }
        //得到作家已出版书的列表
        public List<Bookauthor> GetAuthorBookOuted(string id)
        {
            var bookouted = new List<Bookauthor>();
            using (var dbContext = new pressmannersystemContext())
            {
                bookouted= (from u in dbContext.Bookauthor where u.Aid == id select u).ToList();
                return bookouted;
            }
        }
        //得到作家的出书申请列表
        public List<Fileview> GetAuthorBookFor(string name)
        {
            var bookfor = new List<Fileview>();
            using(var dbContext = new pressmannersystemContext())
            {
                bookfor = (from u in dbContext.Fileview where u.Author == name select u).ToList();
                return bookfor;
            }
        }

        //得到（查询）一个用户的订单数
        public List<Order> GetCustomerOrder(string cid)
        {
            List<Order> orderlist = new List<Order>();
            //string sql = "select * from Order where Cid=cid;";

            using(var dbContext = new pressmannersystemContext())
            {
                //orderlist = dbContext.FromSql(sql).ToList();
                orderlist = (from u in dbContext.Order where u.Cid==cid select u).ToList();
                return orderlist;
                //var blogs = 
            }
        }

        //得到所有的用户
        public List<Customers> GetAllCustomer()
        {
            List<Customers> customerList = new List<Customers>();
            using (var dbContext = new pressmannersystemContext())
            {
                customerList = (from u in dbContext.Customers
                                select u).ToList();
                dbContext.SaveChanges();
            }
            return customerList;
        }

        //得到所有的作者
        public List<Author> GetAllAuthor()
        {
            List<Author> authorList = new List<Author>();
            using (var dbContext = new pressmannersystemContext())
            {
                authorList = (from u in dbContext.Author
                              select u).ToList();
                dbContext.SaveChanges();
            }
            return authorList;
        }

        //删除用户
        public bool RemoveUser(Customers user)
        {
            using (var dbContext= new pressmannersystemContext())
            {
                dbContext.Customers.Remove(user);
                dbContext.SaveChanges();
                return true;
            }
        }
        //得到一本书的所有作者list
        public List<Author> GetAllAuthorOfBook(string isbn)
        {
            var authorlist = new List<Author>();
            var bookauthorlist = new List<Bookauthor>();
            using(var dbContext = new pressmannersystemContext())
            {
                bookauthorlist = (from u in dbContext.Bookauthor where u.Bid == isbn select u).ToList();
                for(int i= 0; i < bookauthorlist.Count; i++)
                {
                    var author = new Author();
                    author = dbContext.Author.FirstOrDefault(x => x.Id == bookauthorlist[i].Aid);
                    authorlist.Add(author);
                }
                return authorlist;
            }
        }

        //查询图书
        public Book QueryBookByISBN(string isbn)
        {
            using (var dbContext= new pressmannersystemContext())
            {
                return dbContext.Book.FirstOrDefault(x => x.Isbn == isbn);
            }
        }

        //查询图书
        public Book QueryBookByName(string name)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                return dbContext.Book.FirstOrDefault(x => x.Name == name);
            }
        }

        //更新图书
        public bool UpdateBook(Book book)
        {
            using (var dbContext= new pressmannersystemContext())
            {
                if (dbContext.Book.Update(book) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //查询作者
        public Author QueryAuthorByID(string id)
        {
            using(var dbContext= new pressmannersystemContext())
            {
                var author = dbContext.Author.FirstOrDefault(x => x.Id == id);
                dbContext.SaveChanges();
                return author;
            }
        }

        //删除作者
        public bool RemoveAuthor(Author author)
        {
            using(var dbContext= new pressmannersystemContext())
            {
                if (dbContext.Author.Remove(author) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //更新作者
        public bool UpdateAuthor(Author oldAuthor)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Author.Update(oldAuthor) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //查询文稿信息BYid
        public Fileview QueryArticleByID(string id)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                var article = dbContext.Fileview.FirstOrDefault(x => x.Id == id);
                dbContext.SaveChanges();
                return article;
            }
        }
        //更新文稿信息
        public bool UpdateArticle(Fileview article)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Fileview.Update(article) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else return false;
            }
        }

        //删除订单
        public bool RemoveOrder(Order order)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Order.Remove(order) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //删除文稿
        public bool RemoveArticle(Fileview article)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Fileview.Remove(article) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //更新订单
        public bool UpdateOrder(Order oldOrder)
        {
            using(var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Order.Update(oldOrder) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }

        //查询订单
        public Order QueryOrderByID(int id)
        {
            using(var dbContext= new pressmannersystemContext())
            {
                   return dbContext.Order.FirstOrDefault(x => x.Id == id);

            }
        }

        //得到一个作者的已出版书
        public List<Book> GetOneAuthorBookOuted(string id)
        {
            var booklist = new List<Book>();
            var bookauthorlist = GetAuthorBookOuted(id);
            for(int i = 0; i < bookauthorlist.Count; i++)
            {
                var book = QueryBookByISBN(bookauthorlist[i].Bid);
                booklist.Add(book);
            }
            return booklist;
        }


        //增加图书
        public bool AddBook(Book book)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Book.Add(book) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
        }
        //删除图书作者
        public bool RemoveAuthorBook(string aid, string isbn)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                //var bookauthor = dbContext.Bookauthor.FirstOrDefault(x => x.Aid == aid && x.Bid == isbn);
                var bookauthors = (from u in dbContext.Bookauthor where u.Aid == aid select u).ToList();
                var bookauthor = bookauthors.Find(x=>x.Bid==isbn);
                if (dbContext.Bookauthor.Remove(bookauthor) != null)
                {
                    dbContext.SaveChanges();
                    return true;
                }
                else return false;
            }
        }
        //增加图书作者信息
        public bool AddAuthorBook(string aid, string isbn)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                if (dbContext.Bookauthor.FirstOrDefault(x => x.Aid == aid && x.Bid == isbn) != null)
                    return false;
                else
                {
                    var bookauthor = new Bookauthor();
                    bookauthor.Aid = aid;
                    bookauthor.Bid = isbn;
                    if (dbContext.Bookauthor.Add(bookauthor) != null)
                    {
                        dbContext.SaveChanges();
                        return true;
                    }
                    else return false;
                }
            }
        }

        //通过图书ISBN查询一本书的信息和其作者信息
        public AuthorOfBook QueryBookAuthorByISBN(string isbn)
        {
            var authorofbook = new AuthorOfBook();
            authorofbook.book = QueryBookByISBN(isbn);
            authorofbook.authors = GetAllAuthorOfBook(isbn);
            return authorofbook;
        }

        //更新用户
        public bool UpdateUser(Customers customers)
        {
            using (var dbContext = new pressmannersystemContext())
            {
                var re = dbContext.Customers.Update(customers);
                dbContext.SaveChanges();
                if (re == null)
                    return false;
                else
                    return true;
            }
        }

        //得到所有订单
        public List<Order> GetAllOrder()
        {
            List<Order> orderList = new List<Order>();
            using (var dbContext = new pressmannersystemContext())
            {
                orderList = (from u in dbContext.Order
                              select u).ToList();
                dbContext.SaveChanges();
            }
            return orderList;
        }

        //得到所有文稿
        public List<Fileview> GetManuscript()
        {
            List<Fileview> file = new List<Fileview>();
            using(var dbContext = new pressmannersystemContext())
            {
                file = (from u in dbContext.Fileview
                        select u).ToList();
                dbContext.SaveChanges();
            }
            return file;
        }

        //得到所有管理员
        public List<Administrator> GetAllAdmin()
        {
            List<Administrator> AdminList = new List<Administrator>();
            using (var dbContext = new pressmannersystemContext())
            {
                AdminList = (from u in dbContext.Administrator
                             select u).ToList();
                dbContext.SaveChanges();
            }
            return AdminList;
        }
    }
}
